{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 使用预定义的estimator处理titanic问题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install tensorflow-gpu==2.0.0a0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.0.0-alpha0\n",
      "sys.version_info(major=3, minor=6, micro=4, releaselevel='final', serial=0)\n",
      "matplotlib 2.2.3\n",
      "numpy 1.16.2\n",
      "pandas 0.22.0\n",
      "sklearn 0.19.1\n",
      "tensorflow 2.0.0-alpha0\n",
      "tensorflow.python.keras.api._v2.keras 2.2.4-tf\n"
     ]
    }
   ],
   "source": [
    "# 导入\n",
    "import matplotlib as mpl\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import sklearn\n",
    "import pandas as pd\n",
    "import os\n",
    "import sys\n",
    "import time\n",
    "import tensorflow as tf\n",
    "from tensorflow import keras\n",
    "\n",
    "print(tf.__version__)\n",
    "print(sys.version_info)\n",
    "for module in mpl,np,pd,sklearn,tf,keras:\n",
    "    print(module.__name__,module.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def solve_cudnn_error():\n",
    "    gpus = tf.config.experimental.list_physical_devices('GPU')\n",
    "    if gpus:\n",
    "        try:\n",
    "            # Currently, memory growth needs to be the same across GPUs\n",
    "            for gpu in gpus:\n",
    "                tf.config.experimental.set_memory_growth(gpu, True)\n",
    "            logical_gpus = tf.config.experimental.list_logical_devices('GPU')\n",
    "            print(len(gpus), \"Physical GPUs,\", len(logical_gpus), \"Logical GPUs\")\n",
    "        except RuntimeError as e:\n",
    "            # Memory growth must be set before GPUs have been initialized\n",
    "            print(e)\n",
    "\n",
    "solve_cudnn_error()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 读取数据，构建数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   survived     sex   age  n_siblings_spouses  parch     fare  class     deck  \\\n",
      "0         0    male  22.0                   1      0   7.2500  Third  unknown   \n",
      "1         1  female  38.0                   1      0  71.2833  First        C   \n",
      "2         1  female  26.0                   0      0   7.9250  Third  unknown   \n",
      "3         1  female  35.0                   1      0  53.1000  First        C   \n",
      "4         0    male  28.0                   0      0   8.4583  Third  unknown   \n",
      "\n",
      "   embark_town alone  \n",
      "0  Southampton     n  \n",
      "1    Cherbourg     n  \n",
      "2  Southampton     y  \n",
      "3  Southampton     n  \n",
      "4   Queenstown     y  \n",
      "   survived     sex   age  n_siblings_spouses  parch     fare   class  \\\n",
      "0         0    male  35.0                   0      0   8.0500   Third   \n",
      "1         0    male  54.0                   0      0  51.8625   First   \n",
      "2         1  female  58.0                   0      0  26.5500   First   \n",
      "3         1  female  55.0                   0      0  16.0000  Second   \n",
      "4         1    male  34.0                   0      0  13.0000  Second   \n",
      "\n",
      "      deck  embark_town alone  \n",
      "0  unknown  Southampton     y  \n",
      "1        E  Southampton     y  \n",
      "2        C  Southampton     y  \n",
      "3  unknown  Southampton     y  \n",
      "4        D  Southampton     y  \n"
     ]
    }
   ],
   "source": [
    "train_file = 'data/titanic/train.csv'\n",
    "eval_file = 'data/titanic/eval.csv'\n",
    "\n",
    "train_df = pd.read_csv(train_file)\n",
    "eval_df = pd.read_csv(eval_file)\n",
    "\n",
    "print(train_df.head())\n",
    "print(eval_df.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      sex   age  n_siblings_spouses  parch     fare  class     deck  \\\n",
      "0    male  22.0                   1      0   7.2500  Third  unknown   \n",
      "1  female  38.0                   1      0  71.2833  First        C   \n",
      "2  female  26.0                   0      0   7.9250  Third  unknown   \n",
      "3  female  35.0                   1      0  53.1000  First        C   \n",
      "4    male  28.0                   0      0   8.4583  Third  unknown   \n",
      "\n",
      "   embark_town alone  \n",
      "0  Southampton     n  \n",
      "1    Cherbourg     n  \n",
      "2  Southampton     y  \n",
      "3  Southampton     n  \n",
      "4   Queenstown     y  \n",
      "      sex   age  n_siblings_spouses  parch     fare   class     deck  \\\n",
      "0    male  35.0                   0      0   8.0500   Third  unknown   \n",
      "1    male  54.0                   0      0  51.8625   First        E   \n",
      "2  female  58.0                   0      0  26.5500   First        C   \n",
      "3  female  55.0                   0      0  16.0000  Second  unknown   \n",
      "4    male  34.0                   0      0  13.0000  Second        D   \n",
      "\n",
      "   embark_town alone  \n",
      "0  Southampton     y  \n",
      "1  Southampton     y  \n",
      "2  Southampton     y  \n",
      "3  Southampton     y  \n",
      "4  Southampton     y  \n",
      "0    0\n",
      "1    1\n",
      "2    1\n",
      "3    1\n",
      "4    0\n",
      "Name: survived, dtype: int64\n",
      "0    0\n",
      "1    0\n",
      "2    1\n",
      "3    1\n",
      "4    1\n",
      "Name: survived, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# 将survived字段取出，赋给label\n",
    "y_train = train_df.pop('survived')\n",
    "y_eval = eval_df.pop('survived')\n",
    "\n",
    "print(train_df.head())\n",
    "print(eval_df.head())\n",
    "print(y_train.head())\n",
    "print(y_eval.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sex ['male' 'female']\n",
      "n_siblings_spouses [1 0 3 4 2 5 8]\n",
      "parch [0 1 2 5 3 4]\n",
      "class ['Third' 'First' 'Second']\n",
      "deck ['unknown' 'C' 'G' 'A' 'B' 'D' 'F' 'E']\n",
      "embark_town ['Southampton' 'Cherbourg' 'Queenstown' 'unknown']\n",
      "alone ['n' 'y']\n"
     ]
    }
   ],
   "source": [
    "# 将数据分成两类：离散和连续\n",
    "categorical_columns = ['sex', 'n_siblings_spouses', 'parch', 'class',\n",
    "                       'deck', 'embark_town', 'alone']\n",
    "numeric_columns = ['age', 'fare']\n",
    "\n",
    "feature_columns = []\n",
    "for categorical_column in categorical_columns:\n",
    "    # 获取离散值所有可能值\n",
    "    vocab = train_df[categorical_column].unique()\n",
    "    print(categorical_column, vocab)\n",
    "    # 对离散值封装，做onehot编码，再加入feature_columns中\n",
    "    feature_columns.append(\n",
    "        tf.feature_column.indicator_column(\n",
    "            tf.feature_column.categorical_column_with_vocabulary_list(\n",
    "                categorical_column, vocab)))\n",
    "    \n",
    "for numeric_column in numeric_columns:\n",
    "    feature_columns.append(\n",
    "        tf.feature_column.numeric_column(numeric_column, dtype=tf.float32))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 从pandas dataframe中构建dataset\n",
    "def make_dataset(data_df, label_df, epochs = 10, shuffle = True, batch_size = 32):\n",
    "    dataset = tf.data.Dataset.from_tensor_slices((dict(data_df), label_df))\n",
    "    if shuffle:\n",
    "        dataset = dataset.shuffle(10000)\n",
    "    dataset = dataset.repeat(epochs).batch(batch_size)\n",
    "    return dataset\n",
    "\n",
    "def make_input_fn(data_df, label_df, epochs = 10, shuffle = True, batch_size = 32):\n",
    "    def input_fn():\n",
    "        dataset = tf.data.Dataset.from_tensor_slices((dict(data_df), label_df))\n",
    "        if shuffle:\n",
    "            dataset = dataset.shuffle(10000)\n",
    "        dataset = dataset.repeat(epochs).batch(batch_size)\n",
    "        return dataset\n",
    "    return input_fn\n",
    "\n",
    "train_input_fn = make_input_fn(train_df, y_train, epochs=100)\n",
    "eval_input_fn = make_input_fn(eval_df, y_eval, epochs=1, shuffle=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用预定义的estimator\n",
    "\n",
    "### BaseLine Estimator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Using default config.\n",
      "INFO:tensorflow:Using config: {'_model_dir': 'baseline_model', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': allow_soft_placement: true\n",
      "graph_options {\n",
      "  rewrite_options {\n",
      "    meta_optimizer_iterations: ONE\n",
      "  }\n",
      "}\n",
      ", '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_train_distribute': None, '_device_fn': None, '_protocol': None, '_eval_distribute': None, '_experimental_distribute': None, '_service': None, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x7f0fd57bf9e8>, '_task_type': 'worker', '_task_id': 0, '_global_id_in_cluster': 0, '_master': '', '_evaluation_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}\n"
     ]
    }
   ],
   "source": [
    "import shutil\n",
    "# 定义文件夹，保存输出的模型\n",
    "output_dir = 'baseline_model'\n",
    "if not os.path.exists(output_dir):\n",
    "    os.mkdir(output_dir)\n",
    "else:\n",
    "    shutil.rmtree(output_dir)\n",
    "    os.mkdir(output_dir)\n",
    "    \n",
    "baseline_estimator = tf.estimator.BaselineClassifier(\n",
    "    model_dir=output_dir, n_classes=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /home/ma-user/anaconda3/envs/TensorFlow-1.13.1/lib/python3.6/site-packages/tensorflow/python/training/training_util.py:238: Variable.initialized_value (from tensorflow.python.ops.variables) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use Variable.read_value. Variables in 2.X are initialized automatically both in eager and graph (inside tf.defun) contexts.\n",
      "INFO:tensorflow:Calling model_fn.\n",
      "WARNING:tensorflow:From /home/ma-user/anaconda3/envs/TensorFlow-1.13.1/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py:883: calling Zeros.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Call initializer instance with the dtype argument instead of passing it to the constructor\n",
      "WARNING:tensorflow:From /home/ma-user/anaconda3/envs/TensorFlow-1.13.1/lib/python3.6/site-packages/tensorflow_estimator/python/estimator/head/base_head.py:546: to_float (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use `tf.cast` instead.\n",
      "INFO:tensorflow:Done calling model_fn.\n",
      "INFO:tensorflow:Create CheckpointSaverHook.\n",
      "INFO:tensorflow:Graph was finalized.\n",
      "INFO:tensorflow:Running local_init_op.\n",
      "INFO:tensorflow:Done running local_init_op.\n",
      "INFO:tensorflow:Saving checkpoints for 0 into baseline_model/model.ckpt.\n",
      "INFO:tensorflow:loss = 0.6931472, step = 0\n",
      "INFO:tensorflow:global_step/sec: 267.446\n",
      "INFO:tensorflow:loss = 0.69781625, step = 100 (0.375 sec)\n",
      "INFO:tensorflow:global_step/sec: 360.908\n",
      "INFO:tensorflow:loss = 0.67727596, step = 200 (0.277 sec)\n",
      "INFO:tensorflow:global_step/sec: 370.339\n",
      "INFO:tensorflow:loss = 0.66348416, step = 300 (0.270 sec)\n",
      "INFO:tensorflow:global_step/sec: 369.907\n",
      "INFO:tensorflow:loss = 0.6768123, step = 400 (0.270 sec)\n",
      "INFO:tensorflow:global_step/sec: 372.947\n",
      "INFO:tensorflow:loss = 0.6913319, step = 500 (0.268 sec)\n",
      "INFO:tensorflow:global_step/sec: 354.757\n",
      "INFO:tensorflow:loss = 0.64715827, step = 600 (0.282 sec)\n",
      "INFO:tensorflow:global_step/sec: 378.397\n",
      "INFO:tensorflow:loss = 0.5905231, step = 700 (0.264 sec)\n",
      "INFO:tensorflow:global_step/sec: 376.477\n",
      "INFO:tensorflow:loss = 0.6618012, step = 800 (0.265 sec)\n",
      "INFO:tensorflow:global_step/sec: 379.322\n",
      "INFO:tensorflow:loss = 0.6761504, step = 900 (0.264 sec)\n",
      "INFO:tensorflow:global_step/sec: 354.33\n",
      "INFO:tensorflow:loss = 0.61766946, step = 1000 (0.282 sec)\n",
      "INFO:tensorflow:global_step/sec: 361.863\n",
      "INFO:tensorflow:loss = 0.6617534, step = 1100 (0.281 sec)\n",
      "INFO:tensorflow:global_step/sec: 360.406\n",
      "INFO:tensorflow:loss = 0.59424174, step = 1200 (0.273 sec)\n",
      "INFO:tensorflow:global_step/sec: 369.19\n",
      "INFO:tensorflow:loss = 0.64848745, step = 1300 (0.271 sec)\n",
      "INFO:tensorflow:global_step/sec: 347.024\n",
      "INFO:tensorflow:loss = 0.61872864, step = 1400 (0.288 sec)\n",
      "INFO:tensorflow:global_step/sec: 367.906\n",
      "INFO:tensorflow:loss = 0.6024939, step = 1500 (0.272 sec)\n",
      "INFO:tensorflow:global_step/sec: 375.776\n",
      "INFO:tensorflow:loss = 0.6898258, step = 1600 (0.266 sec)\n",
      "INFO:tensorflow:global_step/sec: 367.844\n",
      "INFO:tensorflow:loss = 0.6760516, step = 1700 (0.272 sec)\n",
      "INFO:tensorflow:global_step/sec: 361.811\n",
      "INFO:tensorflow:loss = 0.67595446, step = 1800 (0.276 sec)\n",
      "INFO:tensorflow:global_step/sec: 367.056\n",
      "INFO:tensorflow:loss = 0.74949646, step = 1900 (0.272 sec)\n",
      "INFO:tensorflow:Saving checkpoints for 1960 into baseline_model/model.ckpt.\n",
      "INFO:tensorflow:Loss for final step: 0.6427129.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow_estimator.python.estimator.canned.baseline.BaselineClassifierV2 at 0x7f0fd57bf710>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "baseline_estimator.train(\n",
    "    input_fn=lambda : make_dataset(train_df, y_train, epochs=100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Calling model_fn.\n",
      "INFO:tensorflow:Done calling model_fn.\n",
      "INFO:tensorflow:Starting evaluation at 2020-02-07T15:50:40Z\n",
      "INFO:tensorflow:Graph was finalized.\n",
      "WARNING:tensorflow:From /home/ma-user/anaconda3/envs/TensorFlow-1.13.1/lib/python3.6/site-packages/tensorflow/python/training/saver.py:1276: checkpoint_exists (from tensorflow.python.training.checkpoint_management) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use standard file APIs to check for files with this prefix.\n",
      "INFO:tensorflow:Restoring parameters from baseline_model/model.ckpt-1960\n",
      "INFO:tensorflow:Running local_init_op.\n",
      "INFO:tensorflow:Done running local_init_op.\n",
      "INFO:tensorflow:Finished evaluation at 2020-02-07-15:50:41\n",
      "INFO:tensorflow:Saving dict for global step 1960: accuracy = 0.625, accuracy_baseline = 0.625, auc = 0.5, auc_precision_recall = 0.375, average_loss = 0.66182214, global_step = 1960, label/mean = 0.375, loss = 0.6585086, precision = 0.0, prediction/mean = 0.38605928, recall = 0.0\n",
      "INFO:tensorflow:Saving 'checkpoint_path' summary for global step 1960: baseline_model/model.ckpt-1960\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'accuracy': 0.625,\n",
       " 'accuracy_baseline': 0.625,\n",
       " 'auc': 0.5,\n",
       " 'auc_precision_recall': 0.375,\n",
       " 'average_loss': 0.66182214,\n",
       " 'global_step': 1960,\n",
       " 'label/mean': 0.375,\n",
       " 'loss': 0.6585086,\n",
       " 'precision': 0.0,\n",
       " 'prediction/mean': 0.38605928,\n",
       " 'recall': 0.0}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "baseline_estimator.evaluate(\n",
    "    input_fn=lambda : make_dataset(\n",
    "        eval_df, y_eval, epochs=1, shuffle=False, batch_size=20))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Linear Estimator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Using default config.\n",
      "INFO:tensorflow:Using config: {'_model_dir': 'linear_model', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': allow_soft_placement: true\n",
      "graph_options {\n",
      "  rewrite_options {\n",
      "    meta_optimizer_iterations: ONE\n",
      "  }\n",
      "}\n",
      ", '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_train_distribute': None, '_device_fn': None, '_protocol': None, '_eval_distribute': None, '_experimental_distribute': None, '_service': None, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x7f0fc4212cc0>, '_task_type': 'worker', '_task_id': 0, '_global_id_in_cluster': 0, '_master': '', '_evaluation_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}\n"
     ]
    }
   ],
   "source": [
    "import shutil\n",
    "# 定义文件夹，保存输出的模型\n",
    "linear_output_dir = 'linear_model'\n",
    "if not os.path.exists(linear_output_dir):\n",
    "    os.mkdir(linear_output_dir)\n",
    "else:\n",
    "    shutil.rmtree(linear_output_dir)\n",
    "    os.mkdir(linear_output_dir)\n",
    "    \n",
    "linear_estimator = tf.estimator.LinearClassifier(\n",
    "    model_dir=linear_output_dir,\n",
    "    n_classes=2, \n",
    "    feature_columns=feature_columns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Calling model_fn.\n",
      "WARNING:tensorflow:From /home/ma-user/anaconda3/envs/TensorFlow-1.13.1/lib/python3.6/site-packages/tensorflow/python/ops/lookup_ops.py:1347: to_int64 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use `tf.cast` instead.\n",
      "WARNING:tensorflow:From /home/ma-user/anaconda3/envs/TensorFlow-1.13.1/lib/python3.6/site-packages/tensorflow/python/feature_column/feature_column_v2.py:4307: IndicatorColumn._variable_shape (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.\n",
      "WARNING:tensorflow:From /home/ma-user/anaconda3/envs/TensorFlow-1.13.1/lib/python3.6/site-packages/tensorflow/python/feature_column/feature_column_v2.py:4362: VocabularyListCategoricalColumn._num_buckets (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.\n",
      "INFO:tensorflow:Done calling model_fn.\n",
      "INFO:tensorflow:Create CheckpointSaverHook.\n",
      "INFO:tensorflow:Graph was finalized.\n",
      "INFO:tensorflow:Running local_init_op.\n",
      "INFO:tensorflow:Done running local_init_op.\n",
      "INFO:tensorflow:Saving checkpoints for 0 into linear_model/model.ckpt.\n",
      "INFO:tensorflow:loss = 0.6931472, step = 0\n",
      "INFO:tensorflow:global_step/sec: 164.314\n",
      "INFO:tensorflow:loss = 0.38683054, step = 100 (0.610 sec)\n",
      "INFO:tensorflow:global_step/sec: 232.357\n",
      "INFO:tensorflow:loss = 0.45852956, step = 200 (0.431 sec)\n",
      "INFO:tensorflow:global_step/sec: 238.923\n",
      "INFO:tensorflow:loss = 0.41034898, step = 300 (0.418 sec)\n",
      "INFO:tensorflow:global_step/sec: 238.063\n",
      "INFO:tensorflow:loss = 0.5409123, step = 400 (0.420 sec)\n",
      "INFO:tensorflow:global_step/sec: 238.336\n",
      "INFO:tensorflow:loss = 0.26440045, step = 500 (0.419 sec)\n",
      "INFO:tensorflow:global_step/sec: 235.96\n",
      "INFO:tensorflow:loss = 0.33119667, step = 600 (0.424 sec)\n",
      "INFO:tensorflow:global_step/sec: 225.931\n",
      "INFO:tensorflow:loss = 0.49856386, step = 700 (0.442 sec)\n",
      "INFO:tensorflow:global_step/sec: 235.386\n",
      "INFO:tensorflow:loss = 0.36081335, step = 800 (0.425 sec)\n",
      "INFO:tensorflow:global_step/sec: 233.818\n",
      "INFO:tensorflow:loss = 0.5236883, step = 900 (0.427 sec)\n",
      "INFO:tensorflow:global_step/sec: 222.964\n",
      "INFO:tensorflow:loss = 0.34469074, step = 1000 (0.449 sec)\n",
      "INFO:tensorflow:global_step/sec: 233.391\n",
      "INFO:tensorflow:loss = 0.40378848, step = 1100 (0.428 sec)\n",
      "INFO:tensorflow:global_step/sec: 237.038\n",
      "INFO:tensorflow:loss = 0.4174956, step = 1200 (0.422 sec)\n",
      "INFO:tensorflow:global_step/sec: 236.437\n",
      "INFO:tensorflow:loss = 0.26162475, step = 1300 (0.422 sec)\n",
      "INFO:tensorflow:global_step/sec: 235.88\n",
      "INFO:tensorflow:loss = 0.33216512, step = 1400 (0.424 sec)\n",
      "INFO:tensorflow:global_step/sec: 232.338\n",
      "INFO:tensorflow:loss = 0.44482857, step = 1500 (0.430 sec)\n",
      "INFO:tensorflow:global_step/sec: 241.203\n",
      "INFO:tensorflow:loss = 0.27961075, step = 1600 (0.415 sec)\n",
      "INFO:tensorflow:global_step/sec: 233.801\n",
      "INFO:tensorflow:loss = 0.36279163, step = 1700 (0.428 sec)\n",
      "INFO:tensorflow:global_step/sec: 233.951\n",
      "INFO:tensorflow:loss = 0.43807548, step = 1800 (0.431 sec)\n",
      "INFO:tensorflow:global_step/sec: 239.42\n",
      "INFO:tensorflow:loss = 0.40641236, step = 1900 (0.414 sec)\n",
      "INFO:tensorflow:Saving checkpoints for 1960 into linear_model/model.ckpt.\n",
      "INFO:tensorflow:Loss for final step: 0.39564562.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow_estimator.python.estimator.canned.linear.LinearClassifierV2 at 0x7f0fc4212e10>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "linear_estimator.train(\n",
    "    input_fn=lambda : make_dataset(train_df, y_train, epochs=100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Calling model_fn.\n",
      "INFO:tensorflow:Done calling model_fn.\n",
      "INFO:tensorflow:Starting evaluation at 2020-02-07T15:51:10Z\n",
      "INFO:tensorflow:Graph was finalized.\n",
      "INFO:tensorflow:Restoring parameters from linear_model/model.ckpt-1960\n",
      "INFO:tensorflow:Running local_init_op.\n",
      "INFO:tensorflow:Done running local_init_op.\n",
      "INFO:tensorflow:Finished evaluation at 2020-02-07-15:51:11\n",
      "INFO:tensorflow:Saving dict for global step 1960: accuracy = 0.77272725, accuracy_baseline = 0.625, auc = 0.83618003, auc_precision_recall = 0.7919357, average_loss = 0.49561542, global_step = 1960, label/mean = 0.375, loss = 0.4794259, precision = 0.67256635, prediction/mean = 0.4565581, recall = 0.7676768\n",
      "INFO:tensorflow:Saving 'checkpoint_path' summary for global step 1960: linear_model/model.ckpt-1960\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'accuracy': 0.77272725,\n",
       " 'accuracy_baseline': 0.625,\n",
       " 'auc': 0.83618003,\n",
       " 'auc_precision_recall': 0.7919357,\n",
       " 'average_loss': 0.49561542,\n",
       " 'global_step': 1960,\n",
       " 'label/mean': 0.375,\n",
       " 'loss': 0.4794259,\n",
       " 'precision': 0.67256635,\n",
       " 'prediction/mean': 0.4565581,\n",
       " 'recall': 0.7676768}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "linear_estimator.evaluate(\n",
    "    input_fn=lambda : make_dataset(eval_df, y_eval, epochs=1, shuffle=False))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### DNN Estimator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Using default config.\n",
      "INFO:tensorflow:Using config: {'_model_dir': 'dnn_model', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': allow_soft_placement: true\n",
      "graph_options {\n",
      "  rewrite_options {\n",
      "    meta_optimizer_iterations: ONE\n",
      "  }\n",
      "}\n",
      ", '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_train_distribute': None, '_device_fn': None, '_protocol': None, '_eval_distribute': None, '_experimental_distribute': None, '_service': None, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x7f10462e4748>, '_task_type': 'worker', '_task_id': 0, '_global_id_in_cluster': 0, '_master': '', '_evaluation_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}\n"
     ]
    }
   ],
   "source": [
    "import shutil\n",
    "# 定义文件夹，保存输出的模型\n",
    "dnn_output_dir = 'dnn_model'\n",
    "if not os.path.exists(dnn_output_dir):\n",
    "    os.mkdir(dnn_output_dir)\n",
    "else:\n",
    "    shutil.rmtree(dnn_output_dir)\n",
    "    os.mkdir(dnn_output_dir)\n",
    "    \n",
    "dnn_estimator = tf.estimator.DNNClassifier(\n",
    "    model_dir=dnn_output_dir,\n",
    "    n_classes=2,\n",
    "    feature_columns=feature_columns,\n",
    "    hidden_units=[128,128],\n",
    "    activation_fn=tf.nn.relu,\n",
    "    optimizer='Adam')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Calling model_fn.\n",
      "WARNING:tensorflow:From /home/ma-user/anaconda3/envs/TensorFlow-1.13.1/lib/python3.6/site-packages/tensorflow/python/ops/init_ops.py:1257: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Call initializer instance with the dtype argument instead of passing it to the constructor\n",
      "INFO:tensorflow:Done calling model_fn.\n",
      "INFO:tensorflow:Create CheckpointSaverHook.\n",
      "INFO:tensorflow:Graph was finalized.\n",
      "INFO:tensorflow:Running local_init_op.\n",
      "INFO:tensorflow:Done running local_init_op.\n",
      "INFO:tensorflow:Saving checkpoints for 0 into dnn_model/model.ckpt.\n",
      "INFO:tensorflow:loss = 2.5398145, step = 0\n",
      "INFO:tensorflow:global_step/sec: 178.35\n",
      "INFO:tensorflow:loss = 0.66123134, step = 100 (0.562 sec)\n",
      "INFO:tensorflow:global_step/sec: 241.721\n",
      "INFO:tensorflow:loss = 0.4623671, step = 200 (0.414 sec)\n",
      "INFO:tensorflow:global_step/sec: 235.946\n",
      "INFO:tensorflow:loss = 0.31149834, step = 300 (0.424 sec)\n",
      "INFO:tensorflow:global_step/sec: 217.333\n",
      "INFO:tensorflow:loss = 0.3449259, step = 400 (0.460 sec)\n",
      "INFO:tensorflow:global_step/sec: 240.293\n",
      "INFO:tensorflow:loss = 0.37727702, step = 500 (0.416 sec)\n",
      "INFO:tensorflow:global_step/sec: 244.324\n",
      "INFO:tensorflow:loss = 0.411375, step = 600 (0.409 sec)\n",
      "INFO:tensorflow:global_step/sec: 234.061\n",
      "INFO:tensorflow:loss = 0.19414066, step = 700 (0.427 sec)\n",
      "INFO:tensorflow:global_step/sec: 241.708\n",
      "INFO:tensorflow:loss = 0.34738123, step = 800 (0.415 sec)\n",
      "INFO:tensorflow:global_step/sec: 237.843\n",
      "INFO:tensorflow:loss = 0.41883415, step = 900 (0.421 sec)\n",
      "INFO:tensorflow:global_step/sec: 230.183\n",
      "INFO:tensorflow:loss = 0.48878777, step = 1000 (0.434 sec)\n",
      "INFO:tensorflow:global_step/sec: 240.144\n",
      "INFO:tensorflow:loss = 0.2756242, step = 1100 (0.416 sec)\n",
      "INFO:tensorflow:global_step/sec: 234.422\n",
      "INFO:tensorflow:loss = 0.54009044, step = 1200 (0.425 sec)\n",
      "INFO:tensorflow:global_step/sec: 211.874\n",
      "INFO:tensorflow:loss = 0.39561948, step = 1300 (0.474 sec)\n",
      "INFO:tensorflow:global_step/sec: 181.275\n",
      "INFO:tensorflow:loss = 0.33253646, step = 1400 (0.550 sec)\n",
      "INFO:tensorflow:global_step/sec: 204.541\n",
      "INFO:tensorflow:loss = 0.51587814, step = 1500 (0.491 sec)\n",
      "INFO:tensorflow:global_step/sec: 236.898\n",
      "INFO:tensorflow:loss = 0.37594175, step = 1600 (0.420 sec)\n",
      "INFO:tensorflow:global_step/sec: 231.262\n",
      "INFO:tensorflow:loss = 0.35540283, step = 1700 (0.432 sec)\n",
      "INFO:tensorflow:global_step/sec: 240.009\n",
      "INFO:tensorflow:loss = 0.43908873, step = 1800 (0.417 sec)\n",
      "INFO:tensorflow:global_step/sec: 234.543\n",
      "INFO:tensorflow:loss = 0.2089493, step = 1900 (0.426 sec)\n",
      "INFO:tensorflow:Saving checkpoints for 1960 into dnn_model/model.ckpt.\n",
      "INFO:tensorflow:Loss for final step: 0.32376218.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow_estimator.python.estimator.canned.dnn.DNNClassifierV2 at 0x7f10462e45c0>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dnn_estimator.train(\n",
    "    input_fn=lambda : make_dataset(train_df, y_train, epochs=100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Calling model_fn.\n",
      "INFO:tensorflow:Done calling model_fn.\n",
      "INFO:tensorflow:Starting evaluation at 2020-02-07T15:51:42Z\n",
      "INFO:tensorflow:Graph was finalized.\n",
      "INFO:tensorflow:Restoring parameters from dnn_model/model.ckpt-1960\n",
      "INFO:tensorflow:Running local_init_op.\n",
      "INFO:tensorflow:Done running local_init_op.\n",
      "INFO:tensorflow:Finished evaluation at 2020-02-07-15:51:43\n",
      "INFO:tensorflow:Saving dict for global step 1960: accuracy = 0.8030303, accuracy_baseline = 0.625, auc = 0.85561687, auc_precision_recall = 0.81294394, average_loss = 0.47788677, global_step = 1960, label/mean = 0.375, loss = 0.46174872, precision = 0.7196262, prediction/mean = 0.40425703, recall = 0.7777778\n",
      "INFO:tensorflow:Saving 'checkpoint_path' summary for global step 1960: dnn_model/model.ckpt-1960\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'accuracy': 0.8030303,\n",
       " 'accuracy_baseline': 0.625,\n",
       " 'auc': 0.85561687,\n",
       " 'auc_precision_recall': 0.81294394,\n",
       " 'average_loss': 0.47788677,\n",
       " 'global_step': 1960,\n",
       " 'label/mean': 0.375,\n",
       " 'loss': 0.46174872,\n",
       " 'precision': 0.7196262,\n",
       " 'prediction/mean': 0.40425703,\n",
       " 'recall': 0.7777778}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dnn_estimator.evaluate(\n",
    "    input_fn=lambda : make_dataset(eval_df, y_eval, epochs=1, shuffle=False))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 交叉特征\n",
    "cross feature：对两个离散特征做笛卡尔积\n",
    "\n",
    "Eg：\n",
    "\n",
    "age:[1,2,3,4,5], gender:[male, female]\n",
    "\n",
    "age_x_gender:[(1,male),...,(5,male), (1,male),...,(5,female)]\n",
    "\n",
    "100000:100 -> hash(100000 values) % 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "# tf.feature_column.crossed_column 定义交叉特征\n",
    "feature_columns.append(tf.feature_column.indicator_column(\n",
    "    tf.feature_column.crossed_column(['age', 'sex'], hash_bucket_size=100)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Using default config.\n",
      "INFO:tensorflow:Using config: {'_model_dir': 'dnn_model_new_feature', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': allow_soft_placement: true\n",
      "graph_options {\n",
      "  rewrite_options {\n",
      "    meta_optimizer_iterations: ONE\n",
      "  }\n",
      "}\n",
      ", '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_train_distribute': None, '_device_fn': None, '_protocol': None, '_eval_distribute': None, '_experimental_distribute': None, '_service': None, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x7f1046100080>, '_task_type': 'worker', '_task_id': 0, '_global_id_in_cluster': 0, '_master': '', '_evaluation_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}\n"
     ]
    }
   ],
   "source": [
    "import shutil\n",
    "# 定义文件夹，保存输出的模型\n",
    "dnn_output_dir = 'dnn_model_new_feature'\n",
    "if not os.path.exists(dnn_output_dir):\n",
    "    os.mkdir(dnn_output_dir)\n",
    "else:\n",
    "    shutil.rmtree(dnn_output_dir)\n",
    "    os.mkdir(dnn_output_dir)\n",
    "    \n",
    "dnn_estimator_new = tf.estimator.DNNClassifier(\n",
    "    model_dir=dnn_output_dir,\n",
    "    n_classes=2,\n",
    "    feature_columns=feature_columns,\n",
    "    hidden_units=[128,128],\n",
    "    activation_fn=tf.nn.relu,\n",
    "    optimizer='Adam')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Calling model_fn.\n",
      "WARNING:tensorflow:From /home/ma-user/anaconda3/envs/TensorFlow-1.13.1/lib/python3.6/site-packages/tensorflow/python/feature_column/feature_column_v2.py:4362: CrossedColumn._num_buckets (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.\n",
      "INFO:tensorflow:Done calling model_fn.\n",
      "INFO:tensorflow:Create CheckpointSaverHook.\n",
      "INFO:tensorflow:Graph was finalized.\n",
      "INFO:tensorflow:Running local_init_op.\n",
      "INFO:tensorflow:Done running local_init_op.\n",
      "INFO:tensorflow:Saving checkpoints for 0 into dnn_model_new_feature/model.ckpt.\n",
      "INFO:tensorflow:loss = 0.91685367, step = 0\n",
      "INFO:tensorflow:global_step/sec: 171.815\n",
      "INFO:tensorflow:loss = 0.5104016, step = 100 (0.583 sec)\n",
      "INFO:tensorflow:global_step/sec: 234.375\n",
      "INFO:tensorflow:loss = 0.37417728, step = 200 (0.427 sec)\n",
      "INFO:tensorflow:global_step/sec: 229.51\n",
      "INFO:tensorflow:loss = 0.45953017, step = 300 (0.437 sec)\n",
      "INFO:tensorflow:global_step/sec: 233.944\n",
      "INFO:tensorflow:loss = 0.43136257, step = 400 (0.426 sec)\n",
      "INFO:tensorflow:global_step/sec: 217.871\n",
      "INFO:tensorflow:loss = 0.44988418, step = 500 (0.459 sec)\n",
      "INFO:tensorflow:global_step/sec: 234.018\n",
      "INFO:tensorflow:loss = 0.28513223, step = 600 (0.427 sec)\n",
      "INFO:tensorflow:global_step/sec: 227.198\n",
      "INFO:tensorflow:loss = 0.4765982, step = 700 (0.440 sec)\n",
      "INFO:tensorflow:global_step/sec: 232.409\n",
      "INFO:tensorflow:loss = 0.29325318, step = 800 (0.430 sec)\n",
      "INFO:tensorflow:global_step/sec: 235.184\n",
      "INFO:tensorflow:loss = 0.19460596, step = 900 (0.425 sec)\n",
      "INFO:tensorflow:global_step/sec: 219.606\n",
      "INFO:tensorflow:loss = 0.27315086, step = 1000 (0.455 sec)\n",
      "INFO:tensorflow:global_step/sec: 235.392\n",
      "INFO:tensorflow:loss = 0.14662, step = 1100 (0.425 sec)\n",
      "INFO:tensorflow:global_step/sec: 229.76\n",
      "INFO:tensorflow:loss = 0.35847688, step = 1200 (0.435 sec)\n",
      "INFO:tensorflow:global_step/sec: 233.543\n",
      "INFO:tensorflow:loss = 0.22532228, step = 1300 (0.428 sec)\n",
      "INFO:tensorflow:global_step/sec: 237.686\n",
      "INFO:tensorflow:loss = 0.10095565, step = 1400 (0.421 sec)\n",
      "INFO:tensorflow:global_step/sec: 233.609\n",
      "INFO:tensorflow:loss = 0.07696232, step = 1500 (0.428 sec)\n",
      "INFO:tensorflow:global_step/sec: 235.968\n",
      "INFO:tensorflow:loss = 0.20336756, step = 1600 (0.426 sec)\n",
      "INFO:tensorflow:global_step/sec: 223.262\n",
      "INFO:tensorflow:loss = 0.12940109, step = 1700 (0.446 sec)\n",
      "INFO:tensorflow:global_step/sec: 229.541\n",
      "INFO:tensorflow:loss = 0.27732185, step = 1800 (0.435 sec)\n",
      "INFO:tensorflow:global_step/sec: 229.871\n",
      "INFO:tensorflow:loss = 0.1381757, step = 1900 (0.435 sec)\n",
      "INFO:tensorflow:Saving checkpoints for 1960 into dnn_model_new_feature/model.ckpt.\n",
      "INFO:tensorflow:Loss for final step: 0.056624368.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow_estimator.python.estimator.canned.dnn.DNNClassifierV2 at 0x7f1046105e48>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dnn_estimator_new.train(\n",
    "    input_fn=lambda : make_dataset(train_df, y_train, epochs=100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Calling model_fn.\n",
      "INFO:tensorflow:Done calling model_fn.\n",
      "INFO:tensorflow:Starting evaluation at 2020-02-07T15:52:19Z\n",
      "INFO:tensorflow:Graph was finalized.\n",
      "INFO:tensorflow:Restoring parameters from dnn_model_new_feature/model.ckpt-1960\n",
      "INFO:tensorflow:Running local_init_op.\n",
      "INFO:tensorflow:Done running local_init_op.\n",
      "INFO:tensorflow:Finished evaluation at 2020-02-07-15:52:20\n",
      "INFO:tensorflow:Saving dict for global step 1960: accuracy = 0.8030303, accuracy_baseline = 0.625, auc = 0.8413529, auc_precision_recall = 0.78737986, average_loss = 0.65251297, global_step = 1960, label/mean = 0.375, loss = 0.610228, precision = 0.742268, prediction/mean = 0.38915005, recall = 0.72727275\n",
      "INFO:tensorflow:Saving 'checkpoint_path' summary for global step 1960: dnn_model_new_feature/model.ckpt-1960\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'accuracy': 0.8030303,\n",
       " 'accuracy_baseline': 0.625,\n",
       " 'auc': 0.8413529,\n",
       " 'auc_precision_recall': 0.78737986,\n",
       " 'average_loss': 0.65251297,\n",
       " 'global_step': 1960,\n",
       " 'label/mean': 0.375,\n",
       " 'loss': 0.610228,\n",
       " 'precision': 0.742268,\n",
       " 'prediction/mean': 0.38915005,\n",
       " 'recall': 0.72727275}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dnn_estimator_new.evaluate(\n",
    "    input_fn=lambda : make_dataset(eval_df, y_eval, epochs=1, shuffle=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
